<template>
  <!-- 新增录入画面对话框-->
  <el-dialog
    title="分段详细信息"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    @close="closeFn"
    append-to-body
    v-if="openState"
  >
    <el-form ref="editForm" :model="formData" :rules="rules" label-width="110px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="起点金额" prop="lowAmt">
            <el-input v-model="formData.lowAmt" placeholder="" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="截止金额" prop="highAmt">
            <ht-amount-input v-model="formData.highAmt" placeholder=""></ht-amount-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收费模式" prop="feeType">
            <el-select v-model="formData.feeType" placeholder="" @change="change" style="width: 100%">
              <el-option
                v-for="dict in dictMap.FEE_TYPE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="this.formData.feeType !== '1'">
        <el-col :span="12">
          <el-form-item label="段内最低手续费" prop="sFeeMin">
            <ht-amount-input v-model="formData.sFeeMin" placeholder=""></ht-amount-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="段内最高手续费" prop="sFeeMax">
            <ht-amount-input v-model="formData.sFeeMax" placeholder=""></ht-amount-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" v-if="this.formData.feeType !== '2'">
          <el-form-item label="固定金额" prop="fixAmt">
            <ht-amount-input v-model="formData.fixAmt" placeholder=""></ht-amount-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="this.formData.feeType !== '1'">
          <el-form-item label="比例(%)" prop="rate">
            <ht-amount-input v-model="formData.rate" placeholder=""></ht-amount-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" :loading="isLoading">提交</el-button>
      <el-button type="primary" @click="reset">重置</el-button>
    </div>
  </el-dialog>
</template>
<script>
import HtAmountInput from "@/views/components/HtAmountInput";
export default {
  name: "EditInfo",
  components: { HtAmountInput },
  props: {
    dictMap: {
      type: Object,
      default: () => {
        return {};
      },
    },
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  computed: {},
  data() {
    return {
      isLoading: false,
      // 是否显示弹出层
      openState: false,
      // 表单参数
      // 表单校验
      rules: {
        lowAmt: [{ required: true, message: "不能为空", trigger: "blur" }],
        highAmt: [{ required: true, message: "不能为空", trigger: "blur" }],
        feeType: [{ required: true, message: "不能为空", trigger: "change" }],
        sFeeMin: [{ required: true, message: "不能为空", trigger: "blur" }],
        sFeeMax: [{ required: true, message: "不能为空", trigger: "blur" }],
        fixAmt: [{ required: true, message: "不能为空", trigger: "blur" }],
        rate: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },

  methods: {
    change() {
      this.$set(this.formData, "fixAmt", "");
      this.$set(this.formData, "rate", "");
      this.$forceUpdate();
    },

    // 表单重置
    reset() {
      this.resetForm("editForm");
    },
    closeFn() {
      this.openState = false;
      this.reset();
    },
    show() {
      this.openState = true;
    },
    submit() {

      if (eval(this.formData.sFeeMin) > eval(this.formData.sFeeMax)) {
        this.msgError("【最低手续费】不能大于【最高手续费】！");
        return;
      }
      if (eval(this.formData.lowAmt) > eval(this.formData.highAmt)) {
        this.msgError("【截止金额】不能小于【起始金额】！");
        return;
      }
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          if(this.formData.feeType==='1'){
            this.$set(this.formData,"sFeeMin","");
            this.$set(this.formData,"sFeeMax","");
            this.$set(this.formData,"rate","")
          }else if(this.formData.feeType==='2'){
            this.$set(this.formData,"fixAmt","");
          }
          let a = JSON.stringify(this.formData);
          this.$emit("editComplete", a);
          this.closeFn();
        }
      });
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
